//
//  AddressListCell.swift
//  swiftProject
//
//  Created by ZhangSai on 2021/7/7.
//

import UIKit

class AddressListCell: UITableViewCell {

    typealias AddressListCellBlock = (_ cell:AddressListCell,_ tag:Int) -> Void
    var addressListCellBlock : AddressListCellBlock!
    var addressModel : AddressModel!

    @IBOutlet weak var nameLab: UILabel!
    @IBOutlet weak var phoneLab: UILabel!
    @IBOutlet weak var addressLab: UILabel!
    @IBOutlet weak var editBtn: UIButton!
    @IBOutlet weak var deleteBtn: UIButton!
    @IBOutlet weak var hintLab: UILabel!
    @IBOutlet weak var bgView: UIView!
    @IBOutlet weak var selectBtn: UIButton!

    
    override func awakeFromNib() {
        super.awakeFromNib()
        self.backgroundColor = UIColor.init(hexString: "#F3F3F3")
        self.bgView.layer.masksToBounds = true
        self.bgView.layer.cornerRadius = 5
        editBtn.setTitle(ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.WL_edit), for: .normal)
        deleteBtn.setTitle(ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.WL_edit), for: .normal)
        hintLab.text = ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.WL_set_defaultAddress)
        hintLab.isUserInteractionEnabled = true
        
        let tapGes:UITapGestureRecognizer = UITapGestureRecognizer.init(target: self, action: #selector(tapGess))
        hintLab.addGestureRecognizer(tapGes)
        
        self.contentView.layer.shadowColor = UIColor.init(red: 0, green: 0, blue: 0, alpha: 0.2).cgColor
        self.contentView.layer.shadowOffset = CGSize.init(width: 0, height: 1)
        self.contentView.layer.shadowOpacity = 1
        self.contentView.layer.shadowRadius = 2
        self.contentView.layer.cornerRadius = 5
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
    @IBAction func clickAddressSelectBtn(_ sender: UIButton) {
        sender.isSelected = !sender.isSelected
        if (addressListCellBlock != nil) {
            addressModel.isDefault = sender.isSelected ? 1 : 0
            addressListCellBlock(self,sender.tag)
        }
    }
    @objc func tapGess() {
        selectBtn.isSelected = !selectBtn.isSelected
        if (addressListCellBlock != nil) {
            addressModel.isDefault = selectBtn.isSelected ? 1 : 0
            addressListCellBlock(self,selectBtn.tag)
        }
    }
    @IBAction func clickBtnAction(_ sender: UIButton) {
        if (addressListCellBlock != nil) {
            addressListCellBlock(self,selectBtn.tag)
        }
    }
    func setAddressModel(addressModel:AddressModel) {
        self.addressModel = addressModel
        nameLab.text = addressModel.name
        phoneLab.text = addressModel.phone
        addressLab.text = addressModel.city + addressModel.region + addressModel.detailAddress
        self.selectBtn.isSelected = (addressModel.isDefault != 1)
    }

}
